﻿using System;
using WindowsGame.Static;
using WindowsGame.Library.Data;
using WindowsGame.Library.IoC;
using WindowsGame.Library.ProxyObjects;
using WindowsGame.Tests.Data;
using NUnit.Framework;

namespace WindowsGame.Tests.ProxyObjects
{
	[TestFixture]
	public class ProxyObjectBuilderTests
	{
		// System under test.
		private ProxyObjectBuilder proxyObjectBuilder;

		[TestFixtureSetUp]
		public void TestFixtureSetUp()
		{
			IoCContainer.Initialize<IFileProxy, TestFileProxy>();
			IoCContainer.Initialize<IFileManager, FileManager>();

			IoCContainer.Initialize<ProxyObjectBuilder, ProxyObjectBuilder>();
			IoCContainer.Initialize<IProxyObjectManager, ProxyObjectManager>();
		}

		[SetUp]
		public void SetUp()
		{
			proxyObjectBuilder = IoCContainer.Resolve<ProxyObjectBuilder>();
		}

		[Test]
		public void BuildProxyObjectsSpaceshipsTest()
		{
			String path = GetFldr();
			String[] xmlFiles = new[] { "Spaceships.xml" };

			proxyObjectBuilder.BuildProxyObjects(path, xmlFiles);

			Assert.AreEqual(18, proxyObjectBuilder.ProxyObjectManager.ProxyObjects.Count);
		}

		[Test]
		public void BuildProxyObjectsWeaponsTest()
		{
			String path = GetFldr();
			String[] xmlFiles = new[] { "Weapons.xml" };

			proxyObjectBuilder.BuildProxyObjects(path, xmlFiles);

			Assert.AreEqual(10, proxyObjectBuilder.ProxyObjectManager.ProxyObjects.Count);
		}

		[Test]
		public void GetProxyObjectSpaceshipsTest()
		{
			String fldr = GetFldr();
			String[] xmlFiles = new[] { "Spaceships.xml", "Weapons.xml" };

			proxyObjectBuilder.BuildProxyObjects(fldr, xmlFiles);

			String name = "WedgeOneSkinThree";
			ProxyObject proxyObject = proxyObjectBuilder.GetProxyObject(name);

			Assert.AreEqual(name, proxyObject.Name);
		}

		[Test]
		public void GetProxyObjectWeaponsTest()
		{
			String fldr = GetFldr();
			String[] xmlFiles = new[] { "Spaceships.xml", "Weapons.xml" };

			proxyObjectBuilder.BuildProxyObjects(fldr, xmlFiles);

			String name = "DoubleMachineGunTwo";
			ProxyObject proxyObject = proxyObjectBuilder.GetProxyObject(name);

			Assert.AreEqual(name, proxyObject.Name);
		}

		[Test]
		public void GetProxyObjectInvalidTest()
		{
			String fldr = GetFldr();
			String[] xmlFiles = new[] { "Spaceships.xml", "Weapons.xml" };

			proxyObjectBuilder.BuildProxyObjects(fldr, xmlFiles);

			String key = "DummyObject";
			ProxyObject proxyObject = proxyObjectBuilder.GetProxyObject(key);

			Assert.IsNull(proxyObject, key + " is null");
		}

		private static String GetFldr()
		{
			String root = @"..\..\..\..\WindowsGame\WindowsGame\bin\x86\Debug\";
			String path = String.Format("{0}/{1}/{2}", Constants.ROOT_DIRECTORY, Constants.DATA_DIRECTORY, String.Empty);

			return String.Format("{0}{1}", root, path);
		}

		[TestFixtureTearDown]
		public void TestFixtureTearDown()
		{
			IoCContainer.Release();
		}
	}
}